package com.storeroompda.ui.changestore;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.storeroompda.App;
import com.storeroompda.R;
import com.storeroompda.base.model.BaseModel;
import com.storeroompda.bean.ItemBean;
import com.storeroompda.entity.BillCodeControl;
import com.storeroompda.entity.BillCodeInfo;
import com.storeroompda.entity.CodeInfo;
import com.storeroompda.entity.DealerInfo;
import com.storeroompda.tool.AudioControl;
import com.storeroompda.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class ChangeStoreModelImpl extends BaseModel implements ChangeStoreModel {
    private String tag = "ChangeStoreModelImpl";
    private String msg = "";
    private List<BillCodeInfo> list;
    private List<ItemBean> beanList;
    private int scanftype=0;
    private boolean mixsn=false;
    private CodeInfo codeInfo = new CodeInfo();
    private long id;
    BillCodeInfo mbi;

    public ChangeStoreModelImpl() {
        super();
    }

    public ChangeStoreModelImpl(Context context) {
        super(context);
        scanftype=scanfType();
    }

    public void postHandle(final CallBack callBack, final int n) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                switch (n) {
                    case -1:
                        callBack.onshowState(msg);
                        AudioControl.errorSound(context);
                        break;
                    case 0:
                        callBack.onshowList(list);
                        break;
                    case 1:
                        callBack.onshowList(list);
                        msg = Utils.billsuccess;
                        callBack.onshowState(msg);
                        break;
                    case 2:
                        callBack.onshowd(beanList);
                        break;
                    case 3:
                        callBack.onnotifyList(list);
                        callBack.onshowState(msg);
                        break;
                    case 4:
                        callBack.onshowState(msg);
                        break;
                    case 5:
                        callBack.onshowList(list);
                        msg = Utils.billdelsuccess;
                        callBack.onshowState(msg);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    public void showbill(final CallBack callBack) {
        list = BillCodeControl.search(3);
        if (list.size() > 0) {
            list.get(0).setFlag(true);
            for (int i = 1; i < list.size(); i++) {
                list.get(i).setFlag(false);
            }
            postHandle(callBack, 0);
        }
    }

    @Override
    public void addbill(final CallBack callBack, BillCodeInfo bi) {
        long id = BillCodeControl.insert(bi);
        if (id == -1) {
            msg = Utils.billrepeat;
            postHandle(callBack, -1);
        } else {
            list = BillCodeControl.search(3);
            if (list.size() > 0) {
                for (int i = 0; i <list.size() ; i++) {
                    list.get(i).setFlag(false);
                }
                list.get(list.size()-1).setFlag(true);
                postHandle(callBack,1);
            }
        }
    }

    @Override
    public void deletebill(CallBack callBack, BillCodeInfo bi) {
        if(bi==null)
        {
            msg=Utils.billnotexist;
            postHandle(callBack,-1);
            return;
        }
        else {
            BillCodeControl.deleteBill_code(bi);
            BillCodeControl.delete(bi);
            list = BillCodeControl.search(3);
            if(bi.getFlag())
            {
                if(list.size()>0)
                {
                    list.get(0).setFlag(true);
                }
            }
            postHandle(callBack,5);
        }
    }

    @Override
    public void addcode(final CallBack callBack, BillCodeInfo bi, String code) {
        if(bi==null)
        {
            msg=Utils.billnotexist;
            postHandle(callBack,-1);
            return;
        }
        if(!codeJudge(code)) {
            msg = Utils.codelengtherroe;
            postHandle(callBack, -1);
            return;
        }
        switch (scanftype)
        {
            case 1:
                codeInfo = new CodeInfo(null,code,"",bi.getName(),3,bi.getIdd(),bi.getDate());
                id = BillCodeControl.insert(codeInfo);
                if(id==-1)
                {
                    msg=Utils.coderepeat;
                    postHandle(callBack,-1);
                    return;
                }
                else
                {
                    BillCodeControl.update(bi.getId(),1);
                    list=BillCodeControl.search(3);
                    msg=Utils.codeaddsuccess;
                    postHandle(callBack,3);
                }
                break;
            case 2:
                codeInfo = new CodeInfo(null,"",code,bi.getName(),3,bi.getIdd(),bi.getDate());
                id = BillCodeControl.insert(codeInfo);
                if(id==-1)
                {
                    msg=Utils.coderepeat;
                    postHandle(callBack,-1);
                    return;
                }
                else
                {
                    BillCodeControl.update(bi.getId(),1);
                    list=BillCodeControl.search(3);
                    msg=Utils.codeaddsuccess;
                    postHandle(callBack,3);
                }
                break;
            case 3:
                if(!mixsn)
                {
                    if(BillCodeControl.searchCodewl(3,code).size()!=0)
                    {
                        msg=Utils.coderepeat;
                        postHandle(callBack,-1);
                        return;
                    }
                    mbi=bi;
                    codeInfo = new CodeInfo(null,code,"",bi.getName(),3,bi.getIdd(),bi.getDate());
                    id = BillCodeControl.insert(codeInfo);
                    if(id==-1)
                    {
                        msg=Utils.coderepeat;
                        postHandle(callBack,-1);
                        return;
                    }
                    else
                    {
                        msg=Utils.codeaddmixsuccess;
                        postHandle(callBack,4);
                        mixsn=true;
                        return;
                    }
                }
                else
                {
                    if(BillCodeControl.searchCode(3,code).size()!=0)
                    {
                        msg=Utils.coderepeat;
                        postHandle(callBack,-1);
                        return;
                    }
                    else
                    {
                        BillCodeControl.updateCode(id,code);
                        mixsn=false;
                        BillCodeControl.update(mbi.getId(),1);
                        list=BillCodeControl.search(3);
                        msg=Utils.codeaddsuccess;
                        postHandle(callBack,3);
                    }
                }
                break;
        }
    }

    @Override
    public void deletecode(final CallBack callBack, BillCodeInfo bi, String code) {
        if(bi==null)
        {
            msg=Utils.billnotexist;
            postHandle(callBack,-1);
            return;
        }
        if(!codeJudgedel(code))
        {
            msg=Utils.codelengtherroe;
            postHandle(callBack,-1);
            return;
        }
        switch (scanftype)
        {
            case 1:
                codeInfo= BillCodeControl.delete(code,3);
                if(codeInfo==null)
                {
                    msg=Utils.codenotexist;
                    postHandle(callBack,-1);
                    return;
                }
                else
                {
                    for (int i = 0; i <list.size() ; i++) {
                        if(codeInfo.getIid().equals(list.get(i).getIdd()) && codeInfo.getDate().equals(list.get(i).getDate()) && list.get(i).getType()==3)
                        {
                            BillCodeControl.update(list.get(i).getId(),-1);
                            break;
                        }
                    }
                    list=BillCodeControl.search(3);
                    msg=Utils.codedelsuccess;
                    postHandle(callBack,3);
                }
                break;
            case 2:
                codeInfo= BillCodeControl.deletesn(code,3);
                if(codeInfo==null)
                {
                    msg=Utils.codenotexist;
                    postHandle(callBack,-1);
                    return;
                }
                else {
                    for (int i = 0; i < list.size(); i++) {
                        if (codeInfo.getIid().equals(list.get(i).getIdd()) && codeInfo.getDate().equals(list.get(i).getDate()) && list.get(i).getType() == 3) {
                            BillCodeControl.update(list.get(i).getId(), -1);
                            break;
                        }
                    }
                    list = BillCodeControl.search(3);
                    msg = Utils.codedelsuccess;
                    postHandle(callBack,3);
                }
                break;
            case 3:
                codeInfo= BillCodeControl.deletesnORwl(code,3);
                if(codeInfo==null)
                {
                    msg=Utils.codenotexist;
                    postHandle(callBack,-1);
                    return;
                }
                else {
                    for (int i = 0; i < list.size(); i++) {
                        if (codeInfo.getIid().equals(list.get(i).getIdd()) && codeInfo.getDate().equals(list.get(i).getDate()) && list.get(i).getType() == 3) {
                            BillCodeControl.update(list.get(i).getId(), -1);
                            break;
                        }
                    }
                    list = BillCodeControl.search(3);
                    msg = Utils.codedelsuccess;
                    mixsn=false;
                    postHandle(callBack,3);
                }
                break;
        }
    }

    @Override
    public void showd(CallBack callBack) {
        List<DealerInfo> list = BillCodeControl.searchD();
        beanList = new ArrayList<ItemBean>();
        for (int i = 0; i < list.size(); i++) {
            ItemBean itemBean = new ItemBean();
            itemBean.setName(list.get(i).getName());
            itemBean.setId(list.get(i).getIdd());
            beanList.add(itemBean);
        }
        postHandle(callBack, 2);
    }


    private int scanfType() {
        String str = App.sSharedPreferences.getString(Utils.cskey, context.getString(R.string.wlm));
        if (str.equals(context.getString(R.string.wlm))) {
            mixsn = false;
            return 1;
        } else if (str.equals(context.getString(R.string.snm))) {
            mixsn = true;
            return 2;
        } else {
            mixsn = false;
            return 3;
        }
    }

    private boolean codeJudge(String code) {
        int a = App.sSharedPreferences.getInt(Utils.wllkey, 0);
        int b = App.sSharedPreferences.getInt(Utils.wlhkey, 0);
        int c = App.sSharedPreferences.getInt(Utils.snlkey, 0);
        int d = App.sSharedPreferences.getInt(Utils.snhkey, 0);
        String str = App.sSharedPreferences.getString(Utils.cskey, context.getString(R.string.wlm));
        if (str.equals(context.getString(R.string.wlm))) {
            if (code.length() >= a && code.length() <= b) {
                return true;
            } else {
                return false;
            }
        } else if (str.equals(context.getString(R.string.snm))) {
            if (code.length() >= c && code.length() <= d) {
                return true;
            } else {
                return false;
            }
        } else {
            if ((code.length() >= a && code.length() <= b) && (!mixsn)) {
                return true;
            } else if ((code.length() >= c && code.length() <= d) && (mixsn)) {
                return true;
            } else {
                return false;
            }
        }
    }

    private boolean codeJudgedel(String code) {
        int a = App.sSharedPreferences.getInt(Utils.wllkey, 0);
        int b = App.sSharedPreferences.getInt(Utils.wlhkey, 0);
        int c = App.sSharedPreferences.getInt(Utils.snlkey, 0);
        int d = App.sSharedPreferences.getInt(Utils.snhkey, 0);
        String str = App.sSharedPreferences.getString(Utils.cskey, context.getString(R.string.wlm));
        if (str.equals(context.getString(R.string.wlm))) {
            if (code.length() >= a && code.length() <= b) {
                return true;
            } else {
                return false;
            }
        } else if (str.equals(context.getString(R.string.snm))) {
            if (code.length() >= c && code.length() <= d) {
                return true;
            } else {
                return false;
            }
        } else {
            if ((code.length() >= a && code.length() <= b) || (code.length() >= c && code.length() <= d)) {
                return true;
            } else {
                return false;
            }
        }
    }

    String getCodeHint() {
        if (mixsn) {
            return Utils.codesn;
        } else {
            return Utils.codewl;
        }
    }
}



